var orderCache = require('../../../utils/mr_xu/apply_order_cache.js')
var common = require('../../../utils/mr_xu/common.js')
var api = require("../../../utils/mr_xu/api_url.js").api
var request = require("../../../utils/mr_xu/request.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    rows: 20,
    orderString: "createTime.desc",
    items: [],
    status: 11, //11 未完成  12 已完成
    tabs: [{
      label: "未完成",
      status: 11
    },
    {
      label: "已完成",
      status: 12
    }],
    statusConfig: common.statusConfig,
    businessConfig: {
      1: "将车送到指定地点",
      2: "仅找车",
      3: "找车且送到自定地点"
    },
    hasMore : true,
    loading:false
  },
  initSearchParam() {
    this.setData({
      hasMore: true,
      page: 1
    })
  },
  clickTab(evt) {
    this.setData({
      status: evt.currentTarget.dataset.status,
      hasMore:true
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.initSearchParam()
    this.getList()
  },
  onShow: function (options) {
    this.getList()
  },
  detail(res) {
    let id = res.currentTarget.dataset.index
    wx.navigateTo({
      url: '../order_info/order_info?id=' + id,
    })
  },
  getList(append) {

    let that = this
    if (!that.data.hasMore) {
      console.log('no more')
      return
    }

    if (that.data.loading) {
      return
    }

    that.setData({
      loading:true
    })
    
    request.post(api.apply_order_list, {
      data: JSON.stringify({
        page: that.data.page,
        rows: that.data.rows,
        orderString: that.data.orderString,
        status: that.data.status
      })
    }, function(res) {
     
      if (res.obj.rows.length < that.data.rows) {
        that.setData({
          hasMore:false
        })
      }

      if (append && res.obj.rows) {
        let oldItems = that.data.items
        for (let i in res.obj.rows) {
          oldItems.push(res.obj.rows[i])
        }
        that.setData({
          items: oldItems
        })
      } else {
        that.setData({
          items: res.obj.rows,
        })
      }
     
      that.setData({
        loading: false
      })

      wx.stopPullDownRefresh()

    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.initSearchParam()
    this.getList()
  },
  select(res) {
    let that = this
    let index = res.target.dataset.index
    that.setData({
      status: index,
      hasMore:true
    })
    this.getList()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

    if (!this.data.hasMore) {
      return
    }
    
    let page = this.data.page + 1
    console.log('page = ' + page)
    this.setData({
      page: page
    })
    this.getList(true)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})